package com.shycloud.mido.homework.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.homework.api.entity.MidoHomeworkMain;
import com.shycloud.mido.homework.api.entity.TestSharding;
import com.shycloud.mido.homework.api.vo.MusicRankVO;
import com.shycloud.mido.homework.mapper.MidoHomeworkComplateMapper;
import com.shycloud.mido.homework.mapper.MidoHomeworkMainMapper;
import com.shycloud.mido.homework.mapper.TestShardingMapper;
import com.shycloud.mido.homework.service.TestShardingService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Xiaoxing Yu
 * @date 2020/11/17 下午3:36
 **/
@Slf4j
@Service
@AllArgsConstructor
public class TestShardingServiceImpl extends ServiceImpl<TestShardingMapper, TestSharding> implements TestShardingService {

	private TestShardingMapper testShardingMapper;

	private MidoHomeworkMainMapper mainMapper;

	private MidoHomeworkComplateMapper complateMapper;

	public int add(TestSharding entity) {
		return testShardingMapper.insert(entity);
	}

	@Override
	public List<TestSharding> get() {
		return testShardingMapper.selectList(new QueryWrapper<TestSharding>());
	}

	@Override
	public R getSharding(Integer musicId, Long homeworkId) {
		Map returnMap = null;

		try {
			returnMap = new HashMap();

			//Select-One
			if (homeworkId != null) {
				MidoHomeworkMain entity = mainMapper.selectOne(
						new QueryWrapper<MidoHomeworkMain>().eq("id", homeworkId));

				returnMap.put("selectOne", entity);
			}

			if (musicId != null) {
				//Join
				List<MusicRankVO> rankList = complateMapper.getMusicRank(musicId, 10);

				returnMap.put("rankList", rankList);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return new R(CommonConstants.FAIL, "查询信息失败!", false);
		}

		return new R(returnMap);
	}


}
